package com.hsylient.javase.chapter03;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.*;

@Slf4j
public class ArrayListTest {

    private static final ArrayList<String> INSTANCE = new ArrayList<>();

    @Test
    public void testConstructor() {
        ArrayList<String> list = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>(10);
        // 将Collection的实现类作为参数，去创建ArrayList的集合(这个方法可以将别的类型的集合转换为ArrayList)
        HashSet<String> set = new HashSet<>();
        set.add("hsylient");
        ArrayList<String> arrayList = new ArrayList<>(set);
        System.out.println(arrayList);
    }

    @Test
    public void testAdd() {
        INSTANCE.add("hsylient");
        INSTANCE.add(0, "静水流深");
        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("hsylient");
        hashSet.add("hello");
        INSTANCE.addAll(hashSet);
        INSTANCE.addAll(0, hashSet);
        System.out.println(INSTANCE);
    }

    @Test
    public void testRemove() {
        INSTANCE.add("hsylient");
        INSTANCE.clear();
        System.out.println(INSTANCE);
    }

    @Test
    public void testContains() {
        INSTANCE.add("hsylient");
        HashSet<String> strings = new HashSet<>();
        strings.add("hsylient");
        System.out.println(INSTANCE.contains("hsylient"));
        System.out.println(INSTANCE.containsAll(strings));
    }

    /**
     * 返回的是一个不可以修改的集合
     */
    @Test
    public void testCopyOf() {
        INSTANCE.add("hsylient");
        INSTANCE.add("hello");
        List<String> list = List.copyOf(INSTANCE);
        System.out.println(list);
    }

    /**
     * public void replaceAll(UnaryOperator<E> operator)
     * 方法中需要传递一个接口的实现类 可以使用lambda
     * UnaryOperator继承了Function 所以UnaryOperator里边有apply方法
     * 有参数有返回值 x -> String.toUpperCase(x)
     * 满足方法引用的条件 替换为方法引用 String::toUpperCase
     */
    @Test
    public void testReplaceAll() {
        INSTANCE.add("hsylient");
        INSTANCE.replaceAll(String::toLowerCase);
        System.out.println(INSTANCE);
    }

    @Test
    public void testSize() {
        INSTANCE.add("hsylient");
        System.out.println(INSTANCE.size());
    }

    @Test
    public void testSort() {
        INSTANCE.add("31");
        INSTANCE.add("22");
        INSTANCE.add("13");
        INSTANCE.sort((x, y) -> {
            Integer first = Integer.valueOf(x);
            Integer second = Integer.valueOf(y);
            return first - second;
        });
        System.out.println(INSTANCE);
    }

    @Test
    public void testSort2() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.sort(Comparator.comparingInt(x -> x));
        System.out.println(list);
    }

    /**
     * 将这个ArrayList实例的容量修剪为列表的当前大小
     * <p>
     * 比如数组中只有一个元素那么将这个数组的长度改为：1
     */
    @Test
    public void testTrimToSize() {
        INSTANCE.add("hsylient");
        System.out.println(INSTANCE.size());
    }

    @Test
    public void testIsEmpty() {
        System.out.println(INSTANCE.isEmpty());
        ArrayListTest arrayListTest = new ArrayListTest();

    }

    @Test
    public void testRemoveOne() {
        INSTANCE.add("hsylient");
        INSTANCE.add(null);
        INSTANCE.add("hello");
        INSTANCE.add("word");
        INSTANCE.remove("hello");
        System.out.println(INSTANCE.get(2));
    }

    @Test
    public void testIter() {
        INSTANCE.add("hsylient");
        INSTANCE.add(null);
        INSTANCE.add("hello");
        INSTANCE.add("word");
        INSTANCE.remove("hello");
        Iterator<String> iterator = INSTANCE.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        for (String s : INSTANCE) {
            INSTANCE.remove(s);
        }
    }


    /**
     * list转数组
     */
    @Test
    public void testToArray() {
        List<Integer> integers = List.of(1, 2, 3, 4, 5, 6);
        Object[] array = integers.toArray();
        log.info("[测试List的toArray方法] 没有参数的toArray 结果 {}", Arrays.toString(array));
        Integer[] intArray = integers.toArray(new Integer[0]);
        log.info("[测试List的toArray方法] 有参数的toArray {}",Arrays.toString(intArray));
    }
}
